/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.yxw.yxnet_cd_center.common.utils;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.extern.slf4j.Slf4j;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.UUID;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.yxw.yxnet_cd_center.common.constant.CommonConstant;
import com.yxw.yxnet_cd_center.common.result.TokenInfo;
import com.yxw.yxnet_cd_center.common.dto.BaseUserDTO;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
/*
import org.apache.commons.lang3.time.DateUtils;
import com.alibaba.fastjson.JSONObject;
import java.util.Calendar;
import java.util.HashMap;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.interfaces.JWTVerifier;
*/

/**
 * token工具类
 *
 * @author lx
 */
@Slf4j
public class TokenUtil {

    /**
     * jwt 解析token
     *
     * @param token
     * @return
     */
    public static TokenInfo getTokenInfoByToken(String token) {
        DecodedJWT oDecodedJWT = null;
        try {
            oDecodedJWT = JWT.require(Algorithm.HMAC256(CommonConstant.JWT_TOKEN)).build().verify(token);
        } catch (Exception e) {
            log.error("token认证失败,TokenUtil->getTokenInfoByToken  e.getMessage:{}", e.getMessage());
            return null;
        }
        TokenInfo oTokenInfo = getTokenInfo(oDecodedJWT);
        return oTokenInfo;
    }
    public static BaseUserDTO getBaseUserByToken(String token) {
        DecodedJWT oDecodedJWT = null;
        try {
            oDecodedJWT = JWT.require(Algorithm.HMAC256(CommonConstant.JWT_TOKEN)).build().verify(token);
        } catch (Exception e) {
            log.error("token认证失败,TokenUtil->getTokenInfoByToken  e.getMessage:{}", e.getMessage());
            return null;
        }
        TokenInfo oTokenInfo = getTokenInfo(oDecodedJWT);
        BaseUserDTO oBaseUserDTO = new BaseUserDTO();
        oBaseUserDTO.setUserId(oTokenInfo.getUserId().toString());
        oBaseUserDTO.setUserName(oTokenInfo.getUserName());
        oBaseUserDTO.setNickName(oTokenInfo.getNickName());
        oBaseUserDTO.setMobilePhone(oTokenInfo.getMobilePhone());
        return oBaseUserDTO;
    }    
    public static BaseUserDTO getUserNotAuth(String token)
     {
        BaseUserDTO oBaseUserDTO = new BaseUserDTO();
        if(null == token)
        {
            token = "";
        }
        if(token.trim().equals(""))
        {
            Long tmp = System.currentTimeMillis();
            String userTmp = UUID.randomUUID().toString().replaceAll("-", "");
            oBaseUserDTO.setUserId(tmp.toString());
            oBaseUserDTO.setUserName(userTmp.substring(10, 19));
            oBaseUserDTO.setNickName(userTmp.substring(20, 29));
        }
        DecodedJWT oDecodedJWT = null;
        try 
        {
            oDecodedJWT = JWT.decode(token);
        } 
        catch (JWTDecodeException e) 
        {
            System.out.println("TokenUtil->getTokenInfoByTokenNoVerify  e.getMessage():" + e.getMessage());
            return null;
        }
        TokenInfo oTokenInfo = getTokenInfo(oDecodedJWT);
        oBaseUserDTO.setUserId(oTokenInfo.getUserId().toString());
        oBaseUserDTO.setUserName(oTokenInfo.getUserName());
        oBaseUserDTO.setNickName(oTokenInfo.getNickName());
        oBaseUserDTO.setMobilePhone(oTokenInfo.getMobilePhone());
        return oBaseUserDTO;
     }
    private static TokenInfo getTokenInfo(DecodedJWT oDecodedJWT)
    {
        
        Map<String, Claim> map = oDecodedJWT.getClaims();
        Claim oClaim = map.get("token");
        if(null != oClaim)
        {
            String tokenString = oClaim.asString();
            JSONObject oJSONObject = (JSONObject)JSON.parse(tokenString);
            TokenInfo oTokenInfo = new TokenInfo();
            oTokenInfo.setUserId(oJSONObject.getLong("userId"));
            oTokenInfo.setUserName(oJSONObject.getString("userName"));
            oTokenInfo.setNickName(oJSONObject.getString("nickName"));
            oTokenInfo.setMobilePhone(oJSONObject.getString("mobilePhone"));
            oTokenInfo.setExpiresAt(BaseDateTime.getDate(oJSONObject.getString("expirationDate")));
            return oTokenInfo;
        }
        else
        {
            return null;
        }    
    }
}
